// pages/home/home.js
import Service from "../../model/service"
import Category from "../../model/category"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabs: ['全部服务', '在提供', '正在找'],
    categoryList: [],
    serviceList: [],

  },
  parameter: {
    page: '1',
    count: '10'
  },
  totalPages:1,
  handleTabChange: function (event) {
    let index = event.detail.index
  },
  handleCategoryChange(event) {
    console.log(event.currentTarget.dataset.id);
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this._getServiceList()
    this._getCategoryList()
  },
  async _getServiceList() {
    const serviceList = await Service.getServiceList(this.parameter.page,this.parameter.count)
    console.log(serviceList);
    const total= serviceList.total;
    this.totalPages = Math.ceil(total/this.parameter.count)
    console.log(this.totalPages);
    console.log(serviceList);
    this.setData({
      serviceList:[...this.data.serviceList,...serviceList.data]
    })
    console.log(this.data.serviceList);
    wx.stopPullDownRefresh()
  },
  async _getCategoryList() {
    const categoryList = await Category.getCategoryListWithAll()
    this.setData({
      categoryList
    })
  },
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      serviceList:[]
    }),
    this.parameter.page=1;
    this._getServiceList()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function (event) {
    if(this.parameter.page>=this.totalPages){
      wx.showToast({
        title: '没有下一页数据',
      })
      console.log(this.parameter.page);
      console.log("没有下一页数据");
    }else{
      this.parameter.page++;
      this._getServiceList()
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})